/*
 * @FileName: [FinancialReportServiceImpl.java]
 * 
 * @Package com.bcinfo.iccard.service.impl
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement
 * you entered into with RKY.
 * 
 * $Rev$
 * $LastChangedDate$
 * $LastChangedBy$
 * 
 * @category ICCARD
 * 
 * @version 1.1
 * 
 * @author $Author$
 * 
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
 */

package com.bcinfo.iccard.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bcinfo.iccard.dao.ComboCardDailyEarningsDetailMapper;
import com.bcinfo.iccard.dao.ComboCardExpensiveRecordMapper;
import com.bcinfo.iccard.dao.ComboCardExpensiveRecordTempMapper;
import com.bcinfo.iccard.dao.ComboCardMapper;
import com.bcinfo.iccard.dao.ComboCardTotalEarningsMapper;
import com.bcinfo.iccard.dao.DaliyTotalEarningsMapper;
import com.bcinfo.iccard.dao.EndComboCardEarningsToMerchantDetailMapper;
import com.bcinfo.iccard.dao.RechargeableCardMapper;
import com.bcinfo.iccard.dao.RechargeablecardExpensiveRecordMapper;
import com.bcinfo.iccard.dao.RechargeablecardExpensiveRecordTempMapper;
import com.bcinfo.iccard.dao.ToMerchantComboCardEarningsDetailMapper;
import com.bcinfo.iccard.dao.ToMerchantComboCardTotalEarningsMapper;
import com.bcinfo.iccard.dao.ToMerchantDaliyEarningsDetailMapper;
import com.bcinfo.iccard.dao.ToMerchantRechargeableCardTotalEarningsMapper;
import com.bcinfo.iccard.exception.InvalidParametersException;
import com.bcinfo.iccard.model.ComboCard;
import com.bcinfo.iccard.model.ComboCardDailyEarningsDetail;
import com.bcinfo.iccard.model.ComboCardExpensiveRecord;
import com.bcinfo.iccard.model.ComboCardExpensiveRecordTemp;
import com.bcinfo.iccard.model.ComboCardTotalEarnings;
import com.bcinfo.iccard.model.DaliyTotalEarnings;
import com.bcinfo.iccard.model.EndComboCardEarningsToMerchantDetail;
import com.bcinfo.iccard.model.RechargeableCard;
import com.bcinfo.iccard.model.RechargeablecardExpensiveRecord;
import com.bcinfo.iccard.model.RechargeablecardExpensiveRecordTemp;
import com.bcinfo.iccard.model.ToMerchantComboCardEarningsDetail;
import com.bcinfo.iccard.model.ToMerchantComboCardTotalEarnings;
import com.bcinfo.iccard.model.ToMerchantDaliyEarningsDetail;
import com.bcinfo.iccard.model.ToMerchantRechargeableCardTotalEarnings;
import com.bcinfo.iccard.service.FinancialReportService;
import com.bcinfo.iccard.util.PaginationList;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service("financialReportService")
public class FinancialReportServiceImpl implements FinancialReportService
{

    @Autowired
    private ToMerchantDaliyEarningsDetailMapper           _toMerchantDaliyEarningsDetailMapper;

    @Autowired
    private DaliyTotalEarningsMapper                      _daliyTotalEarningsMapper;

    @Autowired
    private ComboCardTotalEarningsMapper                  _comboCardTotalEarningsMapper;

    @Autowired
    private ComboCardDailyEarningsDetailMapper            _comboCardDailyEarningsDetailMapper;

    @Autowired
    private EndComboCardEarningsToMerchantDetailMapper    _endComboCardEarningsToMerchantDetailMapper;

    @Autowired
    private ToMerchantComboCardTotalEarningsMapper        _toMerchantComboCardTotalEarningsMapper;

    @Autowired
    private ToMerchantComboCardEarningsDetailMapper       _toMerchantComboCardEarningsDetailMapper;

    @Autowired
    private ToMerchantRechargeableCardTotalEarningsMapper _toMerchantRechargeableCardTotalEarningsMapper;

    @Autowired
    private RechargeablecardExpensiveRecordTempMapper     _rechargeablecardExpensiveRecordTempMapper;

    @Autowired
    private ComboCardExpensiveRecordTempMapper            _comboCardExpensiveRecordTempMapper;

    @Autowired
    private RechargeableCardMapper                        _rechargeableCardMapper;

    @Autowired
    private RechargeablecardExpensiveRecordMapper         _rechargeablecardExpensiveRecordMapper;

    @Autowired
    private ComboCardExpensiveRecordMapper                _comboCardExpensiveRecordMapper;

    @Autowired
    private ComboCardMapper                               _comboCardMapper;

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param day
     * 
     * @param ipage
     * 
     * @param pagesize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#dailyReport( java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<ToMerchantDaliyEarningsDetail> dailyReport(String sellerCode, String day, int ipage,
            int pagesize)
    {
        PaginationList<ToMerchantDaliyEarningsDetail> plist = new PaginationList<ToMerchantDaliyEarningsDetail>();
        plist.setCurrPage(ipage);
        plist.setPageSize(pagesize);
        if (StringUtils.isBlank(day))
        {
            plist.setRecords(new ArrayList<ToMerchantDaliyEarningsDetail>());
            return plist;
        }
        List<ToMerchantDaliyEarningsDetail> list = _toMerchantDaliyEarningsDetailMapper.selectByMerchantAndDate(
                sellerCode, day, (ipage - 1) * pagesize, pagesize);
        int total = _toMerchantDaliyEarningsDetailMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param month
     * 
     * @param ipage
     * 
     * @param pagesize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#monthlyReport(java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<ToMerchantDaliyEarningsDetail> monthlyReport(String sellerCode, String month, int ipage,
            int pagesize)
    {
        PaginationList<ToMerchantDaliyEarningsDetail> plist = new PaginationList<ToMerchantDaliyEarningsDetail>();
        plist.setCurrPage(ipage);
        plist.setPageSize(pagesize);
        if (StringUtils.isBlank(month))
        {
            plist.setRecords(new ArrayList<ToMerchantDaliyEarningsDetail>());
            return plist;
        }
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
        List<ToMerchantDaliyEarningsDetail> list = _toMerchantDaliyEarningsDetailMapper.selectByMerchantAndMonth(
                sellerCode, month, (ipage - 1) * pagesize, pagesize);
        for (ToMerchantDaliyEarningsDetail data : list)
        {
            try
            {
                data.setDaliyEarningsDate(yyyyMM.parse(month));
            }
            catch (ParseException e)
            {
                e.printStackTrace();
            }
        }
        int total = _toMerchantDaliyEarningsDetailMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param quarter
     * 
     * @param ipage
     * 
     * @param pagesize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#quarterlyReport(java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<ToMerchantDaliyEarningsDetail> quarterlyReport(String sellerCode, String quarter, int ipage,
            int pagesize)
    {
        PaginationList<ToMerchantDaliyEarningsDetail> plist = new PaginationList<ToMerchantDaliyEarningsDetail>();
        plist.setCurrPage(ipage);
        plist.setPageSize(pagesize);
        if (StringUtils.isBlank(quarter))
        {
            plist.setRecords(new ArrayList<ToMerchantDaliyEarningsDetail>());
            return plist;
        }
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
        // 根据季度计算起止日期
        String[] ss = quarter.split("\\.");
        int year = Integer.parseInt(ss[0]);
        int iquarter = Integer.parseInt(ss[1]);
        int startMonth = (iquarter - 1) * 3 + 1;
        int endMonth = iquarter * 3;
        String start = String.valueOf(year * 100 + startMonth);
        String end = String.valueOf(year * 100 + endMonth);
        //
        List<ToMerchantDaliyEarningsDetail> list = _toMerchantDaliyEarningsDetailMapper.selectByMerchantAndQuarter(
                sellerCode, start, end, (ipage - 1) * pagesize, pagesize);
        for (ToMerchantDaliyEarningsDetail data : list)
        {
            try
            {
                data.setDaliyEarningsDate(yyyyMM.parse(String.valueOf(year * 100 + iquarter)));
            }
            catch (ParseException e)
            {
                e.printStackTrace();
            }
        }
        int total = _toMerchantDaliyEarningsDetailMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param year
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#yearlyReport(java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<ToMerchantDaliyEarningsDetail> yearlyReport(String sellerCode, String year, int ipage,
            int pageSize)
    {
        PaginationList<ToMerchantDaliyEarningsDetail> plist = new PaginationList<ToMerchantDaliyEarningsDetail>();
        plist.setCurrPage(ipage);
        plist.setPageSize(pageSize);
        if (StringUtils.isBlank(year))
        {
            plist.setRecords(new ArrayList<ToMerchantDaliyEarningsDetail>());
            return plist;
        }
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
        // 根据年份计算起止日期
        int iyear = Integer.parseInt(year);
        String start = String.valueOf(iyear * 100 + 1);
        String end = String.valueOf(iyear * 100 + 12);
        //
        List<ToMerchantDaliyEarningsDetail> list = _toMerchantDaliyEarningsDetailMapper.selectByMerchantAndQuarter(
                sellerCode, start, end, (ipage - 1) * pageSize, pageSize);
        for (ToMerchantDaliyEarningsDetail data : list)
        {
            try
            {
                data.setDaliyEarningsDate(yyyyMM.parse(String.valueOf(iyear * 100 + 1)));
            }
            catch (ParseException e)
            {
                e.printStackTrace();
            }
        }
        int total = _toMerchantDaliyEarningsDetailMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#queryReport(java.lang.String, java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<ToMerchantDaliyEarningsDetail> queryReport(String sellerCode, String beginTime,
            String endTime, int ipage, int pageSize)
    {

        PaginationList<ToMerchantDaliyEarningsDetail> plist = new PaginationList<ToMerchantDaliyEarningsDetail>();
        plist.setCurrPage(ipage);
        plist.setPageSize(pageSize);
        if (StringUtils.isBlank(beginTime) || StringUtils.isBlank(endTime))
        {
            plist.setRecords(new ArrayList<ToMerchantDaliyEarningsDetail>());
            return plist;
        }
        List<ToMerchantDaliyEarningsDetail> list = _toMerchantDaliyEarningsDetailMapper.selectByMerchantAndDays(
                sellerCode, beginTime, endTime, (ipage - 1) * pageSize, pageSize);
        int total = _toMerchantDaliyEarningsDetailMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#queryReportForChart(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public List<ToMerchantDaliyEarningsDetail> queryReportForChart(String sellerCode, String beginTime, String endTime)
    {

        if (StringUtils.isBlank(beginTime) || StringUtils.isBlank(endTime))
        {
            return new ArrayList<ToMerchantDaliyEarningsDetail>();
        }
        List<ToMerchantDaliyEarningsDetail> list = _toMerchantDaliyEarningsDetailMapper.selectSumByMerchantAndDays(
                sellerCode, beginTime, endTime);
        return list;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#incomeReport(java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<DaliyTotalEarnings> incomeReport(String beginTime, String endTime, int page, int pageSize)
    {
        PaginationList<DaliyTotalEarnings> plist = new PaginationList<DaliyTotalEarnings>();
        plist.setCurrPage(page);
        plist.setPageSize(pageSize);
        List<DaliyTotalEarnings> list = _daliyTotalEarningsMapper.selectByDate(beginTime, endTime, (page - 1)
                * pageSize, pageSize);
        int total = _daliyTotalEarningsMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#incomeReportSum(java.lang.String, java.lang.String)
     */
    @Override
    public DaliyTotalEarnings incomeReportSum(String beginTime, String endTime)
    {
        return _daliyTotalEarningsMapper.selectSumByDate(beginTime, endTime);
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerIds
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#incomeReportQuery(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public ToMerchantDaliyEarningsDetail incomeReportQuery(String sellerIds, String beginTime, String endTime)
    {
        if (StringUtils.isBlank(beginTime) || StringUtils.isBlank(endTime))
            return new ToMerchantDaliyEarningsDetail();
        String[] ids = null;
        if (StringUtils.isNotBlank(sellerIds))
            ids = sellerIds.split(",");
        return _toMerchantDaliyEarningsDetailMapper.selectSumByMerchantsAndDays(ids, beginTime, endTime);
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param packageId
     * 
     * @param status
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#packageReport(java.lang.String, int, int, int)
     */
    @Override
    public PaginationList<ComboCardTotalEarnings> packageReport(String packageId, String beginTime, String endTime,
            int status, int page, int pageSize)
    {
        PaginationList<ComboCardTotalEarnings> plist = new PaginationList<ComboCardTotalEarnings>();
        plist.setCurrPage(page);
        plist.setPageSize(pageSize);
        List<ComboCardTotalEarnings> list = _comboCardTotalEarningsMapper.selectByCombo(packageId, beginTime, endTime,
                status, (page - 1) * pageSize, pageSize);
        int total = _comboCardTotalEarningsMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param cardId
     * 
     * @param sellerCode
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#packageDetail(java.lang.String, java.lang.String, java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<ComboCardDailyEarningsDetail> packageDetail(String cardId, String sellerCode,
            String beginTime, String endTime, int page, int pageSize) throws InvalidParametersException
    {
        if (StringUtils.isBlank(cardId))
            throw new InvalidParametersException("cardId cannot be null.");
        PaginationList<ComboCardDailyEarningsDetail> plist = new PaginationList<ComboCardDailyEarningsDetail>();
        plist.setCurrPage(page);
        plist.setPageSize(pageSize);
        List<ComboCardDailyEarningsDetail> list = _comboCardDailyEarningsDetailMapper.selectByCard(
                Long.parseLong(cardId), sellerCode, beginTime, endTime, (page - 1) * pageSize, pageSize);
        int total = _comboCardDailyEarningsDetailMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param cardId
     * 
     * @param sellerCode
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#packageDetailSum(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public ComboCardDailyEarningsDetail packageDetailSum(String cardId, String sellerCode, String beginTime,
            String endTime) throws InvalidParametersException
    {
        if (StringUtils.isBlank(cardId))
            throw new InvalidParametersException("cardId cannot be null.");
        return _comboCardDailyEarningsDetailMapper.selectSumByCard(cardId, sellerCode, beginTime, endTime);
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param packageId
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @param status
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#packageActSum(java.lang.String, java.lang.String, java.lang.String, int)
     */
    @Override
    public ComboCardTotalEarnings packageActSum(String packageId, String beginTime, String endTime, int status)
    {
        return _comboCardTotalEarningsMapper.selectSumByCombo(packageId, beginTime, endTime, status);
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param cardId
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#packageActDetail(java.lang.String, int, int)
     */
    @Override
    public PaginationList<EndComboCardEarningsToMerchantDetail> packageActDetail(String cardId, int page, int pageSize)
            throws InvalidParametersException
    {
        if (StringUtils.isBlank(cardId))
            throw new InvalidParametersException("cardId cannot be null.");
        PaginationList<EndComboCardEarningsToMerchantDetail> plist = new PaginationList<EndComboCardEarningsToMerchantDetail>();
        plist.setCurrPage(page);
        plist.setPageSize(pageSize);
        List<EndComboCardEarningsToMerchantDetail> list = _endComboCardEarningsToMerchantDetailMapper.selectByCard(
                Long.parseLong(cardId), (page - 1) * pageSize, pageSize);
        int total = _endComboCardEarningsToMerchantDetailMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param cardId
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#packageActDetailCount(java.lang.String)
     */
    @Override
    public int packageActDetailCount(String cardId) throws InvalidParametersException
    {
        if (StringUtils.isBlank(cardId))
            throw new InvalidParametersException("cardId cannot be null.");
        ComboCard card = _comboCardMapper.selectByPrimaryKey(Long.parseLong(cardId));
        return card.getComboCardTotalCopies() - card.getComboCardSurplusCopies();
        // return _endComboCardEarningsToMerchantDetailMapper.selectCountByCard(Long.parseLong(cardId));
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param packageId
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#packageActDetailSum(java.lang.String)
     */
    @Override
    public EndComboCardEarningsToMerchantDetail packageActDetailSum(String cardId) throws InvalidParametersException
    {
        if (StringUtils.isBlank(cardId))
            throw new InvalidParametersException("cardId cannot be null.");
        return _endComboCardEarningsToMerchantDetailMapper.selectSumByCard(Long.parseLong(cardId));
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#sellerIncome(java.lang.String, int, int)
     */
    @Override
    public PaginationList<ToMerchantComboCardTotalEarnings> sellerIncome(String sellerCode, int page, int pageSize)
    {
        PaginationList<ToMerchantComboCardTotalEarnings> plist = new PaginationList<ToMerchantComboCardTotalEarnings>();
        plist.setCurrPage(page);
        plist.setPageSize(pageSize);
        List<ToMerchantComboCardTotalEarnings> list = _toMerchantComboCardTotalEarningsMapper.selectByMerchant(
                sellerCode, (page - 1) * pageSize, pageSize);
        int total = _toMerchantComboCardTotalEarningsMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param dataId
     * 
     * @param param
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @throws InvalidParametersException
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#sellerDetail(java.lang.String, java.lang.String, java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<ToMerchantComboCardEarningsDetail> sellerDetail(String dataId, String cardId,
            String beginTime, String endTime, int page, int pageSize) throws InvalidParametersException
    {
        if (StringUtils.isBlank(dataId))
            throw new InvalidParametersException("dataId cannot be null.");
        PaginationList<ToMerchantComboCardEarningsDetail> plist = new PaginationList<ToMerchantComboCardEarningsDetail>();
        plist.setCurrPage(page);
        plist.setPageSize(pageSize);
        List<ToMerchantComboCardEarningsDetail> list = _toMerchantComboCardEarningsDetailMapper.selectByCard(
                Long.parseLong(dataId), cardId, beginTime, endTime, (page - 1) * pageSize, pageSize);
        int total = _toMerchantComboCardEarningsDetailMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param dataId
     * 
     * @param param
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @return
     * 
     * @throws InvalidParametersException
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#sellerDetailSum(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public ToMerchantComboCardEarningsDetail sellerDetailSum(String dataId, String cardId, String beginTime,
            String endTime) throws InvalidParametersException
    {
        if (StringUtils.isBlank(dataId))
            throw new InvalidParametersException("dataId cannot be null.");
        return _toMerchantComboCardEarningsDetailMapper.selectSumByCard(Long.parseLong(dataId), cardId, beginTime,
                endTime);

    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param dataId
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @return
     * 
     * @throws InvalidParametersException
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#sellerDetailChat(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public List<ToMerchantComboCardEarningsDetail> sellerDetailChat(String dataId, String beginTime, String endTime)
            throws InvalidParametersException
    {
        if (StringUtils.isBlank(dataId))
            throw new InvalidParametersException("dataId cannot be null.");
        return _toMerchantComboCardEarningsDetailMapper.selectSumByCardForChat(Long.parseLong(dataId), beginTime,
                endTime);
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#prepaidCardReport(java.lang.String, int, int)
     */
    @Override
    public PaginationList<ToMerchantRechargeableCardTotalEarnings> prepaidCardReport(String sellerCode, int page,
            int pageSize)
    {
        PaginationList<ToMerchantRechargeableCardTotalEarnings> plist = new PaginationList<ToMerchantRechargeableCardTotalEarnings>();
        plist.setCurrPage(page);
        plist.setPageSize(pageSize);
        List<ToMerchantRechargeableCardTotalEarnings> list = _toMerchantRechargeableCardTotalEarningsMapper
                .selectByMerchant(sellerCode, (page - 1) * pageSize, pageSize);
        int total = _toMerchantRechargeableCardTotalEarningsMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#prepaidCardReportSum(java.lang.String)
     */
    @Override
    public ToMerchantRechargeableCardTotalEarnings prepaidCardReportSum(String sellerCode)
    {
        return _toMerchantRechargeableCardTotalEarningsMapper.selectSumByMerchant(sellerCode);
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#prepaidCardDetailSum(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public ToMerchantDaliyEarningsDetail prepaidCardDetailSum(String sellerCode, String beginTime, String endTime)
            throws InvalidParametersException
    {
        if (StringUtils.isBlank(sellerCode))
            throw new InvalidParametersException("sellerCode cannot be null.");

        return _toMerchantDaliyEarningsDetailMapper.selectSumByMerchantsAndDays(new String[]
        { sellerCode }, beginTime, endTime);
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @throws InvalidParametersException
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#prepaidCardDetail(java.lang.String, java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<ToMerchantDaliyEarningsDetail> prepaidCardDetail(String sellerCode, String beginTime,
            String endTime, int page, int pageSize) throws InvalidParametersException
    {

        PaginationList<ToMerchantDaliyEarningsDetail> plist = new PaginationList<ToMerchantDaliyEarningsDetail>();
        plist.setCurrPage(page);
        plist.setPageSize(pageSize);
        if (StringUtils.isBlank(sellerCode))
        {
            plist.setRecords(new ArrayList<ToMerchantDaliyEarningsDetail>());
            return plist;
        }
        List<ToMerchantDaliyEarningsDetail> list = _toMerchantDaliyEarningsDetailMapper.selectByMerchantAndDays(
                sellerCode, beginTime, endTime, (page - 1) * pageSize, pageSize);
        int total = _toMerchantDaliyEarningsDetailMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param serialNo
     * 
     * @param cardNo
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#prepaidCardManual(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<RechargeablecardExpensiveRecordTemp> prepaidCardManual(String sellerCode, String serialNo,
            String cardNo, String beginTime, String endTime, int page, int pageSize)
    {

        PaginationList<RechargeablecardExpensiveRecordTemp> plist = new PaginationList<RechargeablecardExpensiveRecordTemp>();
        plist.setCurrPage(page);
        plist.setPageSize(pageSize);
        List<RechargeablecardExpensiveRecordTemp> list = _rechargeablecardExpensiveRecordTempMapper.selectByMerchant(
                sellerCode, serialNo, cardNo, beginTime, endTime, (page - 1) * pageSize, pageSize);
        int total = _rechargeablecardExpensiveRecordTempMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param sellerCode
     * 
     * @param serialNo
     * 
     * @param cardNo
     * 
     * @param beginTime
     * 
     * @param endTime
     * 
     * @param page
     * 
     * @param pageSize
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#packageCardManual(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, int, int)
     */
    @Override
    public PaginationList<ComboCardExpensiveRecordTemp> packageCardManual(String sellerCode, String serialNo,
            String cardNo, String beginTime, String endTime, int page, int pageSize)
    {

        PaginationList<ComboCardExpensiveRecordTemp> plist = new PaginationList<ComboCardExpensiveRecordTemp>();
        plist.setCurrPage(page);
        plist.setPageSize(pageSize);
        List<ComboCardExpensiveRecordTemp> list = _comboCardExpensiveRecordTempMapper.selectByMerchant(sellerCode,
                serialNo, cardNo, beginTime, endTime, (page - 1) * pageSize, pageSize);
        int total = _comboCardExpensiveRecordTempMapper.getLastSelectTotalNum();
        plist.setTotalRecord(total);
        plist.setRecords(list);
        return plist;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param id
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#prepaidToCustomer(java.lang.String)
     */
    @Override
    public int prepaidToCustomer(String id)
    {
        // 1.将充值卡临时消费记录表中此条记录状态更改为 3人工干预
        RechargeablecardExpensiveRecordTemp t = _rechargeablecardExpensiveRecordTempMapper.selectByPrimaryKey(Long
                .parseLong(id));
        t.setExpensiveStatus(3);
        _rechargeablecardExpensiveRecordTempMapper.updateByPrimaryKey(t);
        // 2.将涉及的金额返还到此充值卡的余额上
        RechargeableCard card = _rechargeableCardMapper.selectCardByCardNum(t.getRechargeablecardCode());
        card.setRechargeableCardAccountTotal(card.getRechargeableCardAccountTotal() + t.getExpenseAmount());
        _rechargeableCardMapper.updateByPrimaryKey(card);
        return 0;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param id
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#prepaidToSeller(java.lang.String)
     */
    @Override
    public int prepaidToSeller(String id, long operator)
    {
        // 1.将充值卡临时消费记录表中此条记录状态更改为 1已消费
        RechargeablecardExpensiveRecordTemp t = _rechargeablecardExpensiveRecordTempMapper.selectByPrimaryKey(Long
                .parseLong(id));
        t.setExpensiveStatus(1);
        _rechargeablecardExpensiveRecordTempMapper.updateByPrimaryKey(t);
        // 2.在充值卡消费记录表中增加一条记录
        RechargeablecardExpensiveRecord r = new RechargeablecardExpensiveRecord();
        r.setExpensiveDate(new java.util.Date());
        r.setOpUserId(operator);
        r.setRechargeablecardExpensiveTempId(t.getRechargeablecardExpensiveTempId());
        _rechargeablecardExpensiveRecordMapper.insert(r);
        return 0;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param id
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#packageToCustomer(java.lang.String)
     */
    @Override
    public int packageToCustomer(String id)
    {
        // 将套餐卡临时消费记录表中此条记录状态更改为 2人工干预
        ComboCardExpensiveRecordTemp t = _comboCardExpensiveRecordTempMapper.selectByPrimaryKey(Long.parseLong(id));
        t.setExpensiveStatus(2);
        _comboCardExpensiveRecordTempMapper.updateByPrimaryKey(t);
        return 0;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param id
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.FinancialReportService#packageToSeller(java.lang.String)
     */
    @Override
    public int packageToSeller(String id, long operator)
    {
        // 1. 将套餐卡临时消费记录表中此条记录状态更改为 1已消费
        ComboCardExpensiveRecordTemp t = _comboCardExpensiveRecordTempMapper.selectByPrimaryKey(Long.parseLong(id));
        t.setExpensiveStatus(1);
        _comboCardExpensiveRecordTempMapper.updateByPrimaryKey(t);
        // 2.在套餐卡消费记录表中增加一条记录
        ComboCardExpensiveRecord r = new ComboCardExpensiveRecord();
        r.setCombocardExpensiveTempId(t.getCombocardExpensiveTempId());
        r.setExpensiveDate(new java.util.Date());
        r.setOpUserId(operator);
        _comboCardExpensiveRecordMapper.insert(r);
        return 0;
    }

}
